/*
 * Diplomova Prace: Vyuziti embedded modulu pro monitoring zivotnich funkci
 *                  pacienta
 * Trida: Pripojeni.java
 * Autor: Tomas Cinert
 * Vytvoreno: 15.4.2012
 * 
 */
package tcinert;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import tcinert.Zaznam;
import tcinert.Stav;

/**
 * Trida Pripojeni - navazani GPRS spojeni a upload dat
 *
 * @author Tomas Cinert
 * @version 0.9.1
 */
public class Pripojeni implements Runnable {

    HttpConnection con = null;
    InputStream vstupniStream = null;
    DataOutputStream vystupniStream = null;
    Zaznam up;
    Stav odeslano;
    String[][] data;
    boolean loop;

    /**
     * Konstruktor - vytvori http spojeni - otevre vstupni stream pro cteni
     * odezvy
     *
     * @throws IOException - pokud se nepodari otevrit spojeni Exception - pokud
     * neni odezva
     *
     */
    public Pripojeni(Zaznam zaznam, String[][] data, Stav reset) throws Exception {
        con = (HttpConnection) Connector.open("http://test.webingenia.com/ThisIsMe");  // Otevreni spojeni
        con.setRequestMethod(HttpConnection.POST);       // Zaslan pozadavek
        vstupniStream = con.openInputStream();         // Otevreni vstupniho streamu
        this.up = zaznam;
        this.data = data;
        loop = true;
        up.zapis("Zacina upload.", typZapisu.Informativni);
        this.odeslano = reset;

    }
    /**
     * Metoda pro upload dat
     * @throws Exception generovana pri neuspesne odeslani dat
     */
    public void uploadDat() throws Exception // test
    {
        vystupniStream = con.openDataOutputStream();
        for (int i = 0; i < data[0].length; i++) {     // Mozna cyklus volat zvenku
            vystupniStream.writeUTF(data[0][i]);
        }
        if (con.getResponseCode() != HttpConnection.HTTP_OK) // Pokud spojeni probehlo v poradku
        {
            up.zapis("Server nepotvrdil spravne prijeti dat.", typZapisu.Chybovy);
            loop = false;
            throw new IllegalStateException();      // Mozna uplne vynechat .... 
        } else {
            loop = false;
            data = null;     // Data odeslany
        }
        vystupniStream.flush();
        vystupniStream.close();

    }

    /**
     * Metoda na uzavreni spojeni
     * 
     */
    public void uzavreni() {
        try {
            vstupniStream.close();
            con.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }
    /** 
     * Metoda volana pri startu vlakna
     */
    public void run() {
        while (loop) {
            try {
                uploadDat();
                up.zapis("Upload dat probehl uspesne", typZapisu.Informativni);
                odeslano.setData(true);
            } catch (Exception ex) {
                up.zapis("Upload neprobehl z duvodu" + ex.toString(), typZapisu.Chybovy);
                odeslano.setData(false);
            }
            uzavreni();
        }
    }
}
